#!/bin/bash

# init host pre-reboot
# args:
#   $1: hostname: k8s
#   $2: host IP: 192.168.15.130
#   $3: gateway: 192.168.15.2
#   $4: prefix: 24
#   $5: nic name: ens33
#   $6: nameserver: 192.168.15.137

my_hostname=$1
my_ip=$2
my_gateway=$3
my_prefix=$4
eth_name=$5
my_nameserver=$6

# disable selinux, firewalld and swap
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
systemctl disable --now firewalld.service
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab

# disable NetworkManager, dnsmasq, set /etc/resolv.conf
systemctl disable --now NetworkManager
systemctl disable --now dnsmasq

# install base packages and update system
yum install -y epel-release
yum install -y make gcc gcc-c++ sudo patch tk autoconf gettext less vim wget jq psmisc socat yum-utils device-mapper-persistent-data lvm2 ipvsadm ipset sysstat conntrack libseccomp nfs-utils python
yum update -y --exclude=kernel*

# upgrade Linux kernel
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-ml-devel kernel-ml -y
if [[ ! 0 -eq `echo $?` ]]; then
    exit 1
fi
grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfg

# set hostname and /etc/hosts
hostnamectl set-hostname $my_hostname

# set ulimit
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
EOF

#config network
echo $my_ip $(hostname) >> /etc/hosts
cat > /etc/sysconfig/network-scripts/ifcfg-$eth_name <<EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
IPADDR="$my_ip"
PREFIX="$my_prefix"
GATEWAY="$my_gateway"
DNS1="$my_nameserver"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="$eth_name"
DEVICE="$eth_name"
ONBOOT="yes"
EOF

reboot
